
package com.wangzha.business.ctrl;

import com.alibaba.fastjson.JSON;
import com.jfinal.aop.Inject;
import com.jfinal.core.Path;
import com.jfinal.plugin.activerecord.Record;
import com.wangzha.business.service.ApkVersionService;
import com.wangzha.common.base.BaseController;
import com.wangzha.common.model.ApkVersion;
import org.apache.commons.lang3.math.NumberUtils;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 版本更新
 */
@Path("/apk/version")
public class ApkVersionController extends BaseController {

   @Inject
   ApkVersionService service;

   public void index(){
      render("index.html");
   }
    
   public void list() {
      Record record=getAllParamsToRecord();
      renderJson(service.page(getParaToInt("pageNumber", 1), getParaToInt("pageSize", 10),record));
   }
    
   public void add() {
    	render("add.html");
   }

   public void save() {
    	ApkVersion entity = getBean(ApkVersion.class);
        entity.setCreateTime(new Date());
        entity.setUserCode(getVisitor().getCode());
        String[] str = null;
        if (NumberUtils.toInt(entity.getType() + "")== 1) {
           str = getParaValues("apkVersion.fileUrl");
        } else {
           str = getParaValues("apkVersion.apkFileUrl");
        }

        entity.setFileUrl(JSON.toJSONString(removeNullStringArray(str)));
        entity.save();
    	setAttr("apkVersion", entity);
    	render("add.html");
   }

   private List removeNullStringArray(String[] arrayString) {
      List<String> list1 = new ArrayList<String>();
      for (int i=0 ;i<arrayString.length; i++) {
         // 过滤掉数组arrayString里面的空字符串
         if(arrayString[i]!=null && arrayString[i].length()!=0){
            list1.add(arrayString[i]);
         }
      }
      return list1;
   }

   public void edit() {
      setAttr("apkVersion", service.findById(getPara(0)));
      render("edit.html");
   }

   public void update() {
      ApkVersion entity=getBean(ApkVersion.class);
      entity.update();
      setAttr("apkVersion", entity);
      render("edit.html");
   }

   public void delete() {
      service.deleteByIds(getIds());
      renderJson(suc());
   }

}

